import sys
import platform
import os
import psutil
from prettytable import PrettyTable
#Requiere PSUTIL - PRETTYTABLE
#http://code.google.com/p/prettytable/
#http://code.google.com/p/psutil/
sistema = sys.platform
issistema = sistema.startswith("darwin")
if (issistema == True):
	sistema = "Mac OS X"
else:
	issistema = sistema.startswith("linux")
	if (issistema == True):
		sistema = "Linux"
	else:
		issistema = sistema.startswith("win")
		if (issistema == True):
			sistema = "Windows"
		else:
			issistema = sistema.startswith("freebsd")
			if (issistema == True):
				sistema = "FreeBSD"
			else:
				issistema = sistema.startswith("mac")
				if (issistema == True):
					sistema = "Mac OS X"
				else:
					sistema = "Desconocido"

numerocpus = psutil.NUM_CPUS
cpu = str(psutil.cpu_percent())
memoria = str(psutil.virtual_memory())
particiones = str(psutil.disk_usage('/'))
user = str(psutil.get_users())

#Operaciones sobre memoria
mtotal, mavailable, mpercent, mused, mfree, mactive, minactive, mwired = memoria.split(',')
rest, mtotal = mtotal.split('=')
mtotal, rest = mtotal.split('L')
mtotal = (int(mtotal) / 1024) / 1024
rest, mavailable = mavailable.split('=')
mavailable, rest = mavailable.split('L')
mavailable = (int (mavailable) / 1024) / 1024
rest, mpercent = mpercent.split('=')
rest, mused = mused.split('=')
mused, rest = mused.split('L')
mused = (int (mused) / 1024) / 1024
rest, mfree = mfree.split('=')
mfree, rest = mfree.split('L')
mfree = (int (mfree) / 1024) / 1024
rest, mactive = mactive.split('=')
rest, minactive = minactive.split('=')
rest, mwired = mwired.split('=')
mwired, rest = mwired.split(')')

#Operaciones sobre disco
dtotal, dused, dfree, dpercent = particiones.split(',')
rest, dtotal = dtotal.split('=')
dtotal, rest = dtotal.split('L')
dtotal = (int (dtotal) / 1024) / 1024
rest, dused = dused.split('=')
dused, rest = dused.split('L')
dused = (int (dused) / 1024) / 1024
rest, dfree = dfree.split('=')
dfree, rest = dfree.split('L')
dfree = (int (dfree) / 1024) / 1024
rest, dpercent = dpercent.split('=')
dpercent, rest = dpercent.split(')') #Quitamos los simbolos innecesarios.

#Operaciones sobre usuario
uname, rest = user.split(',', 1) #Split primario
rest, uname, rest2 = uname.split('\'') #Split secundario, sobran dos restos.


print " "
print "#GENERAL"
x = PrettyTable(["Op System", "Username"])
x.align["Op System"] = "l"
x.add_row([sistema, uname])
print x 

print " "
print "#CPU"
x = PrettyTable(["Cpus", "Usage"])
x.align["Cpus"] = "l"
x.add_row([numerocpus, cpu])
print x

print " "
print "#MEMORY (MB)"
x = PrettyTable(["Total", "Available", "%", "Used", "Free"])
x.align["Total"] = "l"
x.add_row([mtotal, mavailable, mpercent, mused, mfree])
print x

print " "
print "#DISK SPACE (MB)"
x = PrettyTable(["Total", "Used", "Free", "%"])
x.align["Total"] = "l"
x.add_row([dtotal, dused, dfree, dpercent])
print x


